package com.zxl.always.on.display.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.ClockStyle;
import com.zxl.always.on.display.tools.ImageLoaderManager;
import com.zxl.always.on.display.ui.widget.clock.BaseClockView;

/**
 * Created by Administrator on 2018/3/5.
 */

public class ClockSelectView extends RecyclerView {

    private int mSelectPosition;

    private ClockStyle mClockStyle;

    private ClockAdapter mClockAdapter;

    private TabGroupView.OnItemClickListener mOnItemClickListener;


    public ClockSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false));
        setHasFixedSize(true);
        setAdapter(mClockAdapter = new ClockAdapter());
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void setOnItemClickListener(TabGroupView.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setData(int mSelectPosition, ClockStyle clockStyle) {
        this.mClockStyle = clockStyle;
        this.mSelectPosition = mSelectPosition;
        mClockAdapter.notifyDataSetChanged();
    }

    public ClockStyle getClockStyle() {
        return mClockStyle;
    }

    public void setSelectPosition(int mSelectPosition) {
        this.mSelectPosition = mSelectPosition;
        mClockAdapter.notifyDataSetChanged();
    }

    class ClockAdapter extends RecyclerView.Adapter<ClockViewHolder> {

        @Override
        public ClockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ClockViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.widget_settings_clock_bg_item, parent, false));
        }

        @Override
        public void onBindViewHolder(ClockViewHolder holder, int position) {
            holder.bind(position);
        }

        @Override
        public int getItemCount() {
            return mClockStyle == null ? 0 : mClockStyle.getSize();
        }
    }

    class ClockViewHolder extends RecyclerView.ViewHolder implements OnClickListener, Runnable {

        View itemView;

        private int position;

        private ImageView srcView;

        private View selectView;

        private BaseClockView mBaseClockView;

        public ClockViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            itemView.setOnClickListener(this);
            srcView = itemView.findViewById(R.id.clockstyle_preview_image);
            selectView = itemView.findViewById(R.id.clockstyle_selected_image);
            mBaseClockView = (BaseClockView) LayoutInflater.from(getContext()).inflate(mClockStyle.getLayout(), (ViewGroup) itemView, false);
        }

        public void bind(int position) {
            this.position = position;
            mBaseClockView.setDigitalStyle(position);
            selectView.setVisibility(mSelectPosition == position ? VISIBLE : GONE);
            Bitmap bitmap = ImageLoaderManager.getInstance().loadBitmapFromMemory("clock_select_" + mClockStyle.getLayout() + "_" + position);
            if (null != bitmap) {
                srcView.setImageBitmap(bitmap);
            } else {
                new Thread(this).start();
            }
        }

        @Override
        public void onClick(View v) {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onItemClick(position);
            }
        }

        @Override
        public void run() {
            final Bitmap bitmap = mBaseClockView.getCheckPreview(getContext().getResources().getDisplayMetrics().widthPixels / 4);
            if (mBaseClockView.getDigitalStyle() == position) {
                ImageLoaderManager.getInstance().saveBitmapToMemory("clock_select_" + mClockStyle.getLayout() + "_" + position, bitmap);
                ApplicationHelper.postRunInUiThread(new Runnable() {
                    @Override
                    public void run() {
                        srcView.setImageBitmap(bitmap);
                    }
                });
            }
        }
    }
}
